package com.shanshan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanshan.system.entity.Admin;
import com.shanshan.system.entity.MeetingClass;
import com.shanshan.system.mapper.AdminMapper;
import com.shanshan.system.mapper.BranchMapper;
import com.shanshan.system.mapper.MeetingClassMapper;
import com.shanshan.system.mapper.PeriodMapper;
import com.shanshan.system.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanshan.system.util.JwtUtil;
import com.shanshan.system.util.WebUtil;
import com.shanshan.system.vo.Result;
import com.shanshan.system.vo.SearchAdmin;
import com.shanshan.system.vo.SubmissionRecord;
import com.shanshan.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.shanshan.system.service.impl.MeetingClassServiceImpl.getBetweenDays;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shanshan
 * @since 2023-01-31
 */
@Service
@SuppressWarnings("all")
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private BranchMapper branchMapper;

    @Autowired
    private MeetingClassMapper meetingClassMapper;

    @Autowired
    private PeriodMapper periodMapper;



    /**
     * @param :
     * @return Result
     * @description 得到用户的具体信息
     */
    @Override
    public Result admin() {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        String username = (String) map.get("username");

        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.like("account",username);
        Admin admin = adminMapper.selectOne(wrapper);
        System.out.println("admin++++"+admin);
        return new Result(200,"成功",admin);
    }

    /**
     * @param :
     * @return Result
     * @description 修改用户密码
     */
    @Override
    public Result updateAdmin(Admin admin) {
        LocalDateTime now = LocalDateTime.now();
        admin.setUpdateTime(now);
        int i = adminMapper.updateById(admin);


        if (i==1){
            return new Result(200,"成功");
        }
        return new Result(200,"失败");
    }

    @Override
    public Result selectAdminAll(int currentPage, int pageSize, SearchAdmin searchAdmin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        //判断是否为空
        if (searchAdmin.getName()!=null){
            wrapper.like("name",searchAdmin.getName());
        }
        if (searchAdmin.getPhone()!=null){
            wrapper.like("phone",searchAdmin.getPhone());
        }

        //分页查询
        PageHelper.startPage(currentPage,pageSize);
        List<Admin> admins = adminMapper.selectList(wrapper);
        for (Admin admin : admins) {
            admin.setDept(branchMapper.selectById(admin.getBranchId()).getName());
        }
        PageInfo<Admin> pageInfo = new PageInfo<>(admins);

        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result deleteAdmin(Integer id) {
        UpdateWrapper<Admin> wrapper = new UpdateWrapper<>();
        int i = adminMapper.deleteById(id);
        if (i==1){
            return new Result(200,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result addAdmin(Admin admin) {
        LocalDateTime now = LocalDateTime.now();
        admin.setCreateTime(now);
        int insert = adminMapper.insert(admin);
        if (insert==1){
            return new Result(200,"添加成功");
        }
        return new Result(500,"添加失败");
    }

    @Override
    public Result branchId(Integer id) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("branch_id",id);
        List<Admin> admins = adminMapper.selectList(adminQueryWrapper);
        return new Result(200,"成功",admins);
    }

    @Override
    public Result selectId(Integer id) {
        Admin admin = adminMapper.selectById(id);
        admin.setDept(branchMapper.selectById(admin.getBranchId()).getName());
        return new Result(200,"成功",admin);
    }

    @Override
    public Result accountName(int currentPage, int pageSize, UserVo userVo) {
        PageHelper.startPage(currentPage,pageSize);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("account", userVo.getAccount());
        wrapper.eq("name",userVo.getName());
        Admin admin = adminMapper.selectOne(wrapper);
        Integer branchId = admin.getBranchId();
        QueryWrapper<MeetingClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("branch_id",branchId);
        List<MeetingClass> list = meetingClassMapper.selectList(queryWrapper);

        ArrayList<SubmissionRecord> records = new ArrayList<>();
        int i=1;
        for (MeetingClass aClass : list) {
            String branch = branchMapper.selectById(aClass.getBranchId()).getName();
            String period = periodMapper.selectById(aClass.getPeriodId()).getPeriod();

            //判断是否过期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime nowTime = LocalDateTime.now();
            LocalDateTime createTime = aClass.getCreateTime();
            String endDayStr = nowTime.format(formatter);
            String startDayStr = createTime.format(formatter);


            List<String> timeList = null;
            try {
                timeList = getBetweenDays(startDayStr, endDayStr);
                System.out.println("==================================");
                System.out.println(timeList);
                System.out.println(timeList.size());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (timeList.size()>30){
                aClass.setState(0);
            }else {
                aClass.setState(1);
            }
            if (aClass.getNum1()>aClass.getUsed1()){
                SubmissionRecord record1 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "支部大会",aClass.getNum1()+"/"+aClass.getUsed1(),1,branch,period);
                records.add(record1);
            }else {
                SubmissionRecord record1 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "支部大会",aClass.getNum1()+"/"+aClass.getUsed1(),0,branch,period);
                records.add(record1);
            }

            if (aClass.getNum2()>aClass.getUsed2()){
                SubmissionRecord record2 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "支委会议",aClass.getNum2()+"/"+aClass.getUsed2(),1,branch,period);
                records.add(record2);
            }else {
                SubmissionRecord record2 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "支委会议",aClass.getNum2()+"/"+aClass.getUsed2(),0,branch,period);
                records.add(record2);
            }
            if (aClass.getNum3()>aClass.getUsed3()){
                SubmissionRecord record3 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "党小组会",aClass.getNum3()+"/"+aClass.getUsed3(),1,branch,period);
                records.add(record3);
            }else {
                SubmissionRecord record3 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "党小组会",aClass.getNum3()+"/"+aClass.getUsed3(),0,branch,period);
                records.add(record3);
            }
            if (aClass.getNum4()>aClass.getUsed4()){
                SubmissionRecord record4 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "党课",aClass.getNum4()+"/"+aClass.getUsed4(),1,branch,period);
                records.add(record4);
            }else {
                SubmissionRecord record4 = new SubmissionRecord(i++,aClass.getBranchId(),aClass.getPeriodId(),
                        "党课",aClass.getNum4()+"/"+aClass.getUsed4(),0,branch,period);
                records.add(record4);
            }

        }


        PageInfo<SubmissionRecord> pageInfo = new PageInfo<>(records);
        return new Result(200,"成功",pageInfo);
    }
}
